﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
       <title>Untitled Page</title>
      <script type="text/javascript" language="javascript">     
    
    function addNewItem()
    {       
       
        var mainDiv=document.getElementById('MainDiv');
        
        var newDiv=document.createElement('div');
        newDiv.setAttribute('id','innerDiv');
        newDiv.setAttribute('runat','server');
        
        var newTextBox=document.createElement('input');
        newTextBox.type='text';
        newTextBox.setAttribute('id', 'txtAddr');
        newTextBox.setAttribute('name', 'txtAddr');
        newTextBox.setAttribute('runat','server');
        
        newDiv.appendChild(newTextBox);
        
        mainDiv.appendChild(newDiv);


        return true;
    }    
    function AccessElement()
    {
    
         var elm= document.getElementById("txtAddr");//Returns me the element added dynamically.


         if(elm!=null)
         {
            alert('yes');
         }
    }
 
</script>
</head>
<body>
    <form id="form2" runat="server">        
        <div id="MainDiv" runat="server" >
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <input type="button" value="Create Element" id="b1" onclick="addNewItem();" />   
               <asp:Button ID="btCheckElementIsAdded" runat="server" 
                Text="CheckElementIsAdded" OnClientClick="AccessElement()" 
                onclick="btCheckElementIsAdded_Click"  />
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <div id="QuaDiv" runat="Server">
            </div>
                <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                <asp:HiddenField ID="HiddenField1" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>

</body>
</html>